package com.gin.kotlin.gf2gamecommunity.routes

import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import androidx.paging.compose.collectAsLazyPagingItems
import com.gin.kotlin.gf2gamecommunity.base.composable.Loading
import com.gin.kotlin.gf2gamecommunity.base.composable.MyImage
import com.gin.kotlin.gf2gamecommunity.viewmodel.UserFollowViewModel
import kotlinx.serialization.Serializable


fun NavGraphBuilder.userRelatedList(
    onPopBackStack: () -> Unit,
    onNavigateToUserCenter: (UserCenterRoute) -> Unit,
) = composable<UserRelatedListRoute>(
    // 从屏幕右侧进入，持续500毫秒
    enterTransition = { slideInHorizontally(tween(500)) { it } },
    // 从屏幕右侧退出，持续500毫秒
    exitTransition = { slideOutHorizontally(tween(500)) { it } },
) {
    UserRelatedListComposable(route = it.toRoute(), onPopBackStack = onPopBackStack, onNavigateToUserCenter = onNavigateToUserCenter)
}

/**
 * 列表类型
 * @constructor
 */
enum class ListType(val title: String) {
    FOLLOWS("关注列表"), FANS("粉丝列表")
}

@Serializable
data class UserRelatedListRoute(val userId: Int, val type: ListType)

/**
 * 用户相关列表：关注列表，粉丝列表
 * @param route UserRelatedListRoute
 * @param onPopBackStack () -> Unit
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun UserRelatedListComposable(
    route: UserRelatedListRoute, viewModel: UserFollowViewModel = viewModel(), onPopBackStack: () -> Unit, onNavigateToUserCenter: (UserCenterRoute) -> Unit
) {
    val cachedPagingData = when (route.type) {
        ListType.FOLLOWS -> viewModel.userFollows
        ListType.FANS -> viewModel.userFans
    }
    val lazyPagingItems = cachedPagingData.fetch(route.userId).collectAsLazyPagingItems()

    Scaffold(
        topBar = {
            TopAppBar(navigationIcon = {
                IconButton(onClick = { onPopBackStack() }) {
                    Icon(imageVector = Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "后退")
                }
            }, actions = {

            }, title = { Text(route.type.title) })
        },

        ) { paddingValues ->
        val modifier = Modifier.padding(paddingValues)
        if (lazyPagingItems.itemCount == 0) Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Loading() }
        else {
            LazyVerticalGrid(columns = GridCells.Fixed(2),
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp),
                modifier = modifier.padding(8.dp)) {
                items(lazyPagingItems.itemCount) { page ->
                    lazyPagingItems[page]?.apply {
                        Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier
                            .fillMaxWidth()
                            .clickable {
                                onNavigateToUserCenter(UserCenterRoute(uid))
                            }) {
                            MyImage(model = avatar, modifier = Modifier
                                .clip(CircleShape)
                                .width(80.dp))
                            Text(nickName, fontWeight = FontWeight.Bold)
                        }
                    }
                }
            }
        }
    }
}